package com.csr.csrcheck.controller;

import com.csr.csrcheck.service.ex.Actual_controllerException;
import com.csr.csrcheck.service.ex.ServiceException;
import com.csr.csrcheck.util.JsonResult;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;

public class BaseController {
    public static final int code = 0;

    public static final String OK = "json";

    protected Integer getUidFromSession(HttpSession session) {
        return Integer.valueOf(session.getAttribute("user_id").toString());
    }

    protected String getUsernameFromSession(HttpSession session) {
        return session.getAttribute("user_name").toString();
    }

    @ExceptionHandler({ServiceException.class, FileUploadException.class})
    public JsonResult<Void> handleException(Throwable e) {
        JsonResult<Void> jr = new JsonResult(e);
        if (e instanceof com.csr.csrcheck.service.ex.UsernameConflictException) {
            jr.setcode(Integer.valueOf(4000));
        } else if (e instanceof com.csr.csrcheck.service.ex.UserNotFoundException) {
            jr.setcode(Integer.valueOf(4001));
        } else if (e instanceof com.csr.csrcheck.service.ex.PasswordNotMatchException) {
            jr.setcode(Integer.valueOf(4002));
        } else if (e instanceof com.csr.csrcheck.service.ex.InsertException) {
            jr.setcode(Integer.valueOf(5000));
        } else if (e instanceof com.csr.csrcheck.service.ex.UpdateException) {
            jr.setcode(Integer.valueOf(5001));
        } else if (e instanceof com.csr.csrcheck.controller.ex.FileEmptyException) {
            jr.setcode(Integer.valueOf(6000));
        } else if (e instanceof com.csr.csrcheck.controller.ex.FileSizeException) {
            jr.setcode(Integer.valueOf(6001));
        } else if (e instanceof com.csr.csrcheck.controller.ex.FileTypeException) {
            jr.setcode(Integer.valueOf(6002));
        } else if (e instanceof com.csr.csrcheck.controller.ex.FileUploadStateException) {
            jr.setcode(Integer.valueOf(6003));
        } else if (e instanceof com.csr.csrcheck.controller.ex.FileUploadIOException) {
            jr.setcode(Integer.valueOf(6004));
        } else if (e instanceof com.csr.csrcheck.controller.ex.ProductException) {
            jr.setcode(Integer.valueOf(6005));
        } else if (e instanceof com.csr.csrcheck.service.ex.ProductNotFoundException) {
            jr.setcode(Integer.valueOf(6006));
        } else if (e instanceof com.csr.csrcheck.service.ex.Flight_checkException) {
            jr.setcode(Integer.valueOf(6007));
        } else if (e instanceof com.csr.csrcheck.service.ex.ClinicException) {
            jr.setcode(Integer.valueOf(6008));
        } else if (e instanceof com.csr.csrcheck.service.ex.ClinicException) {
            jr.setcode(Integer.valueOf(6009));
        } else if (e instanceof com.csr.csrcheck.service.ex.ApprovalsException) {
            jr.setcode(Integer.valueOf(7001));
        } else if (e instanceof com.csr.csrcheck.controller.ex.CompanyException) {
            jr.setcode(Integer.valueOf(7002));
        } else if (e instanceof Actual_controllerException) {
            jr.setcode(Integer.valueOf(7003));
        } else if (e instanceof com.csr.csrcheck.service.ex.LawsuitException) {
            jr.setcode(Integer.valueOf(7004));
        }
        return jr;
    }
}
